package highTech.reflect;

import java.lang.reflect.Field;

public class FieldTest4 {
	public static void main(String[] args) throws Exception{
		ReflectPoint poi1=new ReflectPoint(3, 5);
		Field fieldY=poi1.getClass().getField("y");//public
		System.out.println(fieldY.get(poi1));
		
		//Field fieldX=poi1.getClass().getField("x");//private �����ǹ�����Ա�ֶ�   java.lang.NoSuchFieldException
		Field fieldX=poi1.getClass().getDeclaredField("x");//ָ���������ֶ�
		fieldX.setAccessible(true);//��������
		System.out.println(fieldX.get(poi1));
		
		ReflectPoint poi2=new ReflectPoint(4, 6);
		changeStringValue(poi2);
		System.out.println(poi2);
	}

	private static void changeStringValue(ReflectPoint poi2) throws Exception {
		Field[] fields=poi2.getClass().getFields();
		for(Field field:fields){
			if(field.getType()==String.class){//���ڴ����ֽ����ļ�ֻ��һ�ݣ�������==
				String oldValue=(String)field.get(poi2);
				String newValue=oldValue.replace('b', 'a');
				field.set(poi2, newValue);
			}
		}
		
	}

}
